package com.pro.h5case.admin.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pro.h5case.admin.service.AdminService;
import com.pro.h5case.common.model.BAdmin;
import com.pro.h5case.common.model.BDictionary;
import com.pro.h5case.common.model.BPlan;
import com.pro.h5case.common.model.LogInfoEntity;
import com.pro.h5case.common.utils.Common;

import sun.misc.BASE64Decoder;

@Controller
@RequestMapping("/admin")
public class AdminController {
	@Resource
	private AdminService adminService;

	/**
	 * 登录
	 */
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String to(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {

			return "admin/login";
		
	}
	/**
	 * 返回指示板index
	 */
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String back(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			return "admin/index";
		
	}
	/**
	 * 登出
	 */
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String out(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		session.setAttribute("admin", null);
			return "admin/login";
		
	}
	/**
	 * 登录
	 */
	@RequestMapping(value = "/doLogin", method = RequestMethod.POST)
	public String toSg(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,@RequestParam("name") String name,@RequestParam("password") String password)
			throws Exception {
		Map<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("adName", name);
		parameter.put("adPassword", password);
		
		List<BAdmin> tag= adminService.selectAdminByMap(parameter);
		//判断
		if(tag !=null && tag.size()>=1){
			session.setAttribute("admin", tag.get(0));
			return "redirect:index";
		}else{
			model.addAttribute("errorTag", "error");
			return "admin/login";
		}
		
	}
	/**
	 * 新增
	 */
	@RequestMapping(value = "/toAdd", method = RequestMethod.GET)
	public String insert(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
			return "admin/adminManage/add";
		
	}
	/**
	 * 新增
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,BAdmin admin)
			throws Exception {
		Date now = new Date();
		admin.setAdCreateTime(now);
		admin.setAdDeleteState((byte) 1);
		int tag = adminService.insertAdmin(admin);
		model.addAttribute("subTag", tag);
			return "admin/adminManage/add";
		
	}
	/**
	 * 修改
	 */
	@RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
	@ResponseBody
	public int updatePassword(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,@RequestParam("adId")int id,@RequestParam("newPassword")String password)
			throws Exception {
		int tag = -1;
		Map<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("adId", id);
		parameter.put("adPassword", password);
		tag = adminService.updateAdminByMap(parameter);
		if(tag == 0){
			tag = -1;
		}else if(tag == 1){
			BAdmin admin = (BAdmin) session.getAttribute("admin");
			admin.setAdPassword(password);
			session.setAttribute("admin", admin);
		}
		
			return tag;
		
	}
	/**
	 * 修改
	 */
	@RequestMapping(value = "/toUpdate", method = RequestMethod.GET)
	public String toUpdate(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,@RequestParam("adminId")int id)
			throws Exception {
		
		BAdmin admin = adminService.selectByPirmaryKey(id);
		model.addAttribute("ad", admin);
			return "admin/adminManage/update";
		
	}
	/**
	 * 修改
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,BAdmin admin)
			throws Exception {
		
		int tag = adminService.updateAdmin(admin);
		if(tag == 0){
			tag = -1;
		}
		model.addAttribute("upTag", tag);
		
			return "forward: list";
		
	}
	/**
	 * 删除
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public String delete(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,@RequestParam("id[]") int [] ids)
			throws Exception {
		int num = 0;
		out:for (int i = 0; i < ids.length; i++) {
			num = adminService.deleteAdminByPrimaryKey(ids[i]);
			if(num == 0){
				num = -1;
				break out;
			}
		}
		
		model.addAttribute("deleteTag", num);
			return "forward: list";
		
	}
	/**
	 * 列表页面
	 */
	@RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
	public String list(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			Map<String,Object> parameter = new HashMap<String, Object>();
			
			parameter.put("begin", 0);
			parameter.put("size", 15);
			List<BAdmin> adminList = adminService.selectAdminByMap(parameter);
			
			model.addAttribute("adminList", adminList);
			int planSum = adminService.countAdminByMap(parameter);
			int pageSum = planSum / 15;
			if(planSum%15 == 0){
				
			}else{
				pageSum+=1;
			}
			model.addAttribute("pageNo", 1);
			model.addAttribute("pageSum", pageSum);
			return "admin/adminManage/list";
		
	}
	/**
	 * 翻页
	 */
	@RequestMapping(value = "/newPage", method = RequestMethod.POST)
	@ResponseBody
	public Object newPage(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,@RequestParam("pageNo") int pageNo,@RequestParam("key") String key)
			throws Exception {

			Map<String,Object> parameter = new HashMap<String, Object>();
			parameter.clear();
			parameter.put("begin", pageNo*15);
			parameter.put("size", 15);
			parameter.put("sort", key);
			List<BAdmin> adminList = adminService.selectAdminByMap(parameter);
			
			model.addAttribute("adminList", adminList);
			int planSum = adminService.countAdminByMap(parameter);
			
			int pageSum = planSum / 15;
			if(planSum%15 == 0){
				
			}else{
				pageSum+=1;
			}
			parameter.clear();
			parameter.put("adminList", adminList);
			parameter.put("pageSum", pageSum);
			parameter.put("pageNo", pageNo);
			return parameter;
		
	}
}
